package com.team233.zuccecho.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.team233.zuccecho.Bean.*;
import com.team233.zuccecho.dto.FeedbackDao;
import com.team233.zuccecho.rabbitMQ.ZuccEchoMessage;
import com.team233.zuccecho.repository.*;
import com.team233.zuccecho.result.ExceptionMsg;
import com.team233.zuccecho.result.ResponseData;
import com.team233.zuccecho.service.inter.CourseInterface;
import com.team233.zuccecho.service.inter.StudentInterface;
import com.team233.zuccecho.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.RandomStringUtils;


import java.sql.Timestamp;
import java.util.*;

@RestController
@RequestMapping("v1")
public class StudentController {
    @Autowired
    private StudentInterface studentInterface;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private FanoutExchange fanout;

    private final Logger logger = LoggerFactory.getLogger(StudentController.class);

    //获取所有问卷
    @RequestMapping(value = "getquestionnaires",method = RequestMethod.GET)
    public ResponseData getQn(long stu_id){
        List<String> info = studentInterface.getQns(stu_id);
        return new ResponseData(ExceptionMsg.SUCCESS,info.toString());
    }

    //获取指定问卷
    @RequestMapping(value = "getquestionnaire",method = RequestMethod.GET)
    public ResponseData getSpeQn(long qn_id){
        List<Question> questions = studentInterface.getQn(qn_id);
        return new ResponseData(ExceptionMsg.SUCCESS,questions);
    }

    //学生获取个人课表
    @GetMapping("student/clazzIfo/{stu_id}")
    public ResponseData QueryStuClazz(@PathVariable("stu_id") long stu_id){
        List<Clazz> courseList = studentInterface.QueryStuClazz(stu_id);
        return new ResponseData(ExceptionMsg.SUCCESS, courseList);
    }

    //学生提交问卷
    @PostMapping("submitquestionnaire")
    public void submit(@RequestBody FeedbackDao feedbackDao){

        // 2.工作模式 收到答卷，传入消息队列
        ZuccEchoMessage msg = new ZuccEchoMessage(ZuccEchoMessage.CATEGORY_FB);
        msg.appendContent("feedback",feedbackDao);
        amqpTemplate.convertAndSend(Constants.QUE_WORK_QUEUE, msg);

        // 3.发布订阅模式 学生提交问卷，通知老师和教学督导组
       // logger.warn("deal with the paper"); // 还在写的状态

        // 模拟很多的问卷到达，无实际功能
//        ZuccEchoMessage msg = new ZuccEchoMessage(ZuccEchoMessage.CATEGORY_PAPER);
//        msg.appendContent("paper", RandomStringUtils.randomNumeric(5));
//        amqpTemplate.convertAndSend(fanout.getName(), "", msg);

//        return new ResponseData(ExceptionMsg.SUCCESS);
    }

    //学生获取反馈列表
    @GetMapping("stu/getfeedbacks/{student_id}")
    public ResponseData StuHaveFeedback(@PathVariable("student_id") long student_id){
        List<Feedback> feedbackList= studentInterface.findFeedBack(student_id);
        return new ResponseData(ExceptionMsg.SUCCESS,feedbackList);
    }

}
